package com.li.algorithm.leetcode.hot100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Test2 {

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        System.out.println(test2.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        int j = 0;
        HashMap<String, Integer> map = new HashMap<>();
        List<List<String>> result = new ArrayList<>(1000);
        for (int i = 0; i < strs.length; i++) {
            String s1 = strs[i];
            char[] chars = new char[s1.length()];
            s1.getChars(0, s1.length(), chars, 0);
            Arrays.sort(chars);
            String sorted = Arrays.toString(chars);
            if (map.containsKey(sorted)) {
                Integer num = map.get(sorted);
                List<String> list = result.get(num);
                list.add(s1);
            } else {
                List<String> list = new ArrayList<>();
                list.add(s1);
                result.add(list);
                map.put(sorted, j);
                j++;
            }
        }
        return result;
    }




}
